package brainfsck

import chisel3._
import chisel3.util._
import scala.io.Source
import java.nio.file.{Files, Paths}
import scala.util.{Try, Using}

object MiFile {
    def emitMiString[T <: Data](content: Seq[T]): String = {
        "#File_format=Hex\n" +
        "#Address_depth=" + content.length.toString + "\n" +
        "#Data_width=" + content(1).getWidth.toString + "\n" +
        content.map(instr => "%x\n".format(instr.asUInt.litValue)).mkString
    }

    def emitMiFile[T <: Data](content: Seq[T], filePath: String): Try[Unit] = {
        val path = Paths.get(filePath)
        Files.createDirectories(path.getParent())
        Using(Files.newBufferedWriter(path)) { writer =>
            writer.write(MiFile.emitMiString(content))
        }
    }
}
